package me.jessyan.mvparms.demo.mvp.ui.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.di.component.login.DaggerFindPassAndBindSetComponent;
import me.jessyan.mvparms.demo.di.module.login.FindPassAndBindSetModule;
import me.jessyan.mvparms.demo.mvp.contract.login.FindPassAndBindSetContract;
import me.jessyan.mvparms.demo.mvp.presenter.login.FindPassAndBindSetPresenter;
import me.jessyan.mvparms.demo.mvp.ui.widget.MyVerifyLinerText;
import me.jessyan.mvparms.demo.mvp.ui.widget.PassWordEditText;
import me.jessyan.mvparms.demo.mvp.ui.widget.PhoneEditText;

import static com.jess.arms.utils.Preconditions.checkNotNull;
import static me.jessyan.mvparms.demo.mvp.presenter.login.FindPassAndBindSetPresenter.FIND_PASSWORD;
import static me.jessyan.mvparms.demo.mvp.presenter.login.FindPassAndBindSetPresenter.SET;
import static me.jessyan.mvparms.demo.mvp.presenter.login.FindPassAndBindSetPresenter.VERIFY;

/**
 * 找回密码
 * 验证手机
 * 设置密码
 */
public class FindPassAndBindSetActivity extends BaseActivity<FindPassAndBindSetPresenter> implements FindPassAndBindSetContract.View {

    @BindView(R.id.phone_et)
    PhoneEditText phoneEt;
    @BindView(R.id.verify_ll)
    MyVerifyLinerText verifyLl;
    @BindView(R.id.passWord_one_et)
    PassWordEditText passWordOneEt;
    @BindView(R.id.passWord_two_et)
    PassWordEditText passWordTwoEt;
    @BindView(R.id.login_tv)
    TextView loginTv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    /**
     * 输入框些的输入是否满足需求
     */
    private boolean phoneOk, verifyOk, passWordOneOk, passWordTwoOk;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {

        DaggerFindPassAndBindSetComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .findPassAndBindSetModule(new FindPassAndBindSetModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        mPresenter.PAGE_TYPE = getIntent().getIntExtra("page_type", -1);
        return R.layout.activity_find_pass_and_bind_set; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        intUI();
        initListener();
    }

    private void intUI() {
        if (mPresenter.PAGE_TYPE == FIND_PASSWORD) {
            phoneEt.setVisibility(View.VISIBLE);
            verifyLl.setVisibility(View.VISIBLE);
            passWordOneEt.setVisibility(View.VISIBLE);
            passWordTwoEt.setVisibility(View.VISIBLE);
            loginTv.setText("提交");
            titleTv.setText("找回密码");
        } else if (mPresenter.PAGE_TYPE == VERIFY) {
            phoneEt.setVisibility(View.VISIBLE);
            verifyLl.setVisibility(View.VISIBLE);
            passWordOneEt.setVisibility(View.GONE);
            passWordTwoEt.setVisibility(View.GONE);
            loginTv.setText("验证");
            titleTv.setText("验证手机");
        } else {
            phoneEt.setVisibility(View.GONE);
            verifyLl.setVisibility(View.GONE);
            passWordOneEt.setVisibility(View.VISIBLE);
            passWordTwoEt.setVisibility(View.VISIBLE);
            loginTv.setText("提交");
            titleTv.setText("设置密码");
        }
    }

    private void initListener() {

        // 手机号输入框
        phoneEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //当12位时向后又输入一位达到加上空格十三位
                if (s.length() == 13) {
                    phoneOk = true;
                } else {
                    phoneOk = false;
                }
                checkLoginTvClickable();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //验证码
        verifyLl.setVerifyListener(new MyVerifyLinerText.VerifyListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 4) {
                    verifyOk = true;
                } else {
                    verifyOk = false;
                }
                checkLoginTvClickable();
            }

            @Override
            public void onClick(View view) {
                verifyLl.startTimer();
            }
        });

        //密码输入框
        //password是否有输入
        passWordOneEt.setInputLisenter(new PassWordEditText.InputLisenter() {

            @Override
            public void inputChange(boolean isInput) {
                passWordOneOk = isInput;
                checkLoginTvClickable();
            }
        });
        passWordTwoEt.setInputLisenter(new PassWordEditText.InputLisenter() {

            @Override
            public void inputChange(boolean isInput) {
                passWordTwoOk = isInput;
                checkLoginTvClickable();
            }
        });


    }

    private void checkLoginTvClickable() {
        boolean enable = false;
        switch (mPresenter.PAGE_TYPE) {
            case FIND_PASSWORD:
                if (phoneOk && verifyOk && passWordOneOk && passWordTwoOk) {
                    enable = true;
                }
                break;
            case VERIFY:
                if (phoneOk && verifyOk) {
                    enable = true;
                }
                break;
            case SET:
                if (passWordOneOk && passWordTwoOk) {
                    enable = true;
                }
                break;
                default:enable = false;

        }
        loginTv.setEnabled(enable);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }




    @OnClick({R.id.back_iv, R.id.login_tv})
    public void onViewClicked(View view) {
//        if(isDoubleClick(view))return;
        switch (view.getId()) {

            case R.id.back_iv:
                killMyself();
                break;
            case R.id.login_tv:
                if(mPresenter.PAGE_TYPE==VERIFY){
                    Intent intent =new Intent(FindPassAndBindSetActivity.this,FindPassAndBindSetActivity.class);
                    intent.putExtra("page_type", SET);
                    startActivity(intent);
                }
                break;
        }
    }
}
